<?php
require_once("sys_conf.inc");
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
if (isset($_POST["media"]))
{
	$media = $_POST["media"];
	$link_id = mysql_connect($DBHOST,$DBUSER,$DBPWD);
	mysql_select_db($DBNAME);
	mysql_query("SET NAMES 'utf8'");
	$id = $media["id"];
	$subjectItemId = $media["oid"];
	$title = strip_tags($media["title"]);
	$mediaType = $media["mediaType"];
	$listVisible = $media["listVisible"];

	//上传处理
	$sort=12;
	$f_type=strtolower("gif,jpg,jpeg,swf,flv");//设置可上传的文件类型 
	$file_size_max=200*1024*1024;//限制单个文件上传最大容量 
	$overwrite = 0;//是否允许覆盖相同文件,1:允许,0:不允许 
	$f_input="media";//设置上传域名称
	$attdir="../../file/";
	foreach($_FILES[$f_input]["error"] as $key => $error){ 
		$up_error="no"; 
		if ($error == UPLOAD_ERR_OK){ 
			$f_name=$_FILES[$f_input]['name'][$key];//获取上传源文件名 
			$uploadfile=$uploaddir.strtolower(basename($f_name)); 
      $tmp_type=substr(strrchr($f_name,"."),1);//获取文件扩展名
			$tmp_type=strtolower($tmp_type);			 
			if(!stristr($f_type,$tmp_type)){ 
				echo "<script>alert('对不起,不能上传".$tmp_type."格式文件, ".$f_name." 文件上传失败!')</script>"; 
        $up_error="yes"; 
      }
			switch($tmp_type){
				case 'gif':
				$mediaType == '0' ? $mediaType : 0;
				case 'jpg':
				$mediaType == '0' ? $mediaType : 0;
				case 'jpeg':
				$mediaType == '0' ? $mediaType : 0;
				case 'flv':
				$mediaType == '1' ? $mediaType : 1;
				case 'swf':
				$mediaType == '2' ? $mediaType : 2;
			}
			if ($_FILES[$f_input]['size'][$key]>$file_size_max) { 
				echo "<script>alert('对不起,你上传的文件 ".$f_name." 容量为".round($_FILES[$f_input]['size'][$key]/1024)."Kb,大于规定的".($file_size_max/1024)."Kb,上传失败!')</script>"; 
      	$up_error="yes"; 
      }
			if (file_exists($uploadfile)&&!$overwrite){ 
      	echo "<script>alert('对不起,文件 ".$f_name." 已经存在,上传失败!')</script>"; 
        $up_error="yes"; 
      }
			$string = 'abcdefghijklmnopgrstuvwxyz0123456789';
			$rand = '';  
			for($x=0;$x<$sort;$x++)
  			$rand .= substr($string,mt_rand(0,strlen($string)-1),1); 
			$t=date("ymdHis").substr($gettime[0],2,6).$rand;
			if(!is_dir($attdir))
			{
				mkdir($attdir);
			}
			$uploadfile=$attdir.$t.".".$tmp_type; 
      if(($up_error!="yes") and (move_uploaded_file($_FILES[$f_input]['tmp_name'][$key], $uploadfile)))
			{ 
				$_msg=$_msg.$f_name.'上传成功\n';
				$path = str_replace("../..","",$uploadfile);
				echo "<script>alert('".$_msg."');</script>";				
			} 
			else{
				$_msg=$_msg.$f_name.'上传失败\n';
				echo "<script>alert('".$_msg."');</script>";		
			}			
	  } 
	}
	if(!$id){
		$addTime = date("Y-m-d H:i:s");
		$sql = "insert into extpollmedia(subjectItemId,title,path,mediaType,listVisible,addTime) values('$subjectItemId','$title','$path','$mediaType','$listVisible','$addTime')";
		mysql_query($sql);
		echo "<script>alert('添加成功!');</script>";
	}else{
		$sql = "UPDATE extpollmedia SET title = '$title',path = '$path',mediaType = '$mediaType', listVisible = '$listVisible' WHERE id = '$id'";
		mysql_query($sql);
		echo "<script>alert('修改成功!');</script>";
	} 
}
?>
